﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField]
    private PlayerBody m_playerModel;

    private List<PlayerBody> m_playerList;



    private void Start()
    {
        if (m_playerList == null) m_playerList = new List<PlayerBody>();
        //NetWorkController.instance.Receive.NewMsgEvent_Login += m_OnSomeOneLogin;
        //NetWorkController.instance.Receive.NewMsgEvent_Logout += m_OnSomeOneLogout;
        //NetWorkController.instance.Receive.NewMsgEvent_Direction += m_OnSomeOneDirection;
        //NetWorkController.instance.Receive.NewMsgEvent_Split += m_OnSomeOneSplit;
        //NetWorkController.instance.Receive.NewMsgEvent_DeathBtn += m_OnSomeOneDeathBtn;

    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            var info = new LoginInfo();
            info.userId = "my user id";
            info.nickName = "Quaye";
            info.headPortraitUrl = @"http://mp.weixin.qq.com/s/BG2Hbt1d9XvO3fW7IDkNBA";
            m_OnSomeOneLogin(null, info);
        }

        if (Input.GetKeyDown(KeyCode.L))
        {

            m_OnSomeOneSplit(null, new DirectionInfo() { userId = "my user id" });
        }

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            foreach (var c in m_playerList) c.SetMoveDirection(Vector2.up);
        }
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            foreach (var c in m_playerList) c.SetMoveDirection(Vector2.down);
        }
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            foreach (var c in m_playerList) c.SetMoveDirection(Vector2.left);
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            foreach (var c in m_playerList) c.SetMoveDirection(Vector2.right);
        }
    }
    private void m_OnSomeOneLogin(object sender, EventArgs e)
    {
        //LoginInfo loginInfo = e as LoginInfo;
        ////重复登陆判断
        //Debug.Log("login Info in player controller :" + loginInfo.userId);
        //CustomInfo_LoginRes loginMsg = new CustomInfo_LoginRes();
        //PlayerBody tempUser = m_FindUser(loginInfo.userId);
        //if (tempUser == null)
        //{
        //    Debug.Log("login : " + loginInfo.userId + "\n" +
        //              "headPortrait:" + loginInfo.headPortraitUrl + "\n" +
        //              "nickName:" + loginInfo.nickName);

        //    tempUser = Instantiate(m_playerModel.gameObject, this.transform, false).GetComponent<PlayerBody>();
        //    tempUser.Init(loginInfo.userId, loginInfo.nickName, loginInfo.headPortraitUrl, 0);
        //    m_playerList.Add(tempUser);
        //    loginMsg.SetMsg(PUSH_MESSAGE_TYPE.CUSTOM_LONGIN_T, null);
        //    //tempUser.DeathEvent += m_SndMsgWhenSomeOneDeath;
        //    //tempUser.FindEnemyEvent += m_FindEnemyCallBack;
        //}
        //else
        //{
        //    Debug.LogWarning("Relogin  !! " + tempUser.userId);
        //    loginMsg.SetMsg(PUSH_MESSAGE_TYPE.CUSTOM_LONGIN_RT, tempUser);
        //}
        //tempUser.gameObject.SetActive(true);
        //string _msg = loginMsg.ToJson();
        //// NetWorkController.instance.Send.PushMsgToOther(_msg, new string[] { loginInfo.userId });
    }
    private void m_OnSomeOneLogout(object sender, EventArgs e)
    {
        LogoutInfo logOutInfo = e as LogoutInfo;
        PlayerBody tempUser = m_FindUser(logOutInfo.userId);
        if (tempUser == null)
        {
            Debug.Log("logOutInfo :" + logOutInfo.userId);
            Debug.LogWarning("some one is not logout ,but recv a logout info !!");
        }
        else
        {
            tempUser.gameObject.SetActive(false);
            // m_UIRankList.SetUsersInfo(m_RankThePlayer(m_playerList));
        }
    }


    private void m_OnSomeOneDirection(object sender, EventArgs e)
    {
        DirectionInfo dirInfo = e as DirectionInfo;
        PlayerBody tempUser = m_FindUser(dirInfo.userId);
        if (tempUser == null)
        {
            Debug.LogWarning("Recv Dir message ,but not find the user !! userId : " + dirInfo.userId);
        }
        else
        {
            tempUser.SetMoveDirection(new Vector2(dirInfo.dir_x, dirInfo.dir_y));
            Debug.Log("Dir userId :" + dirInfo.userId);
        }
    }


    private void m_OnSomeOneSplit(object sender, EventArgs e)
    {
        DirectionInfo dirInfo = e as DirectionInfo;
        PlayerBody tempUser = m_FindUser(dirInfo.userId);
        if (tempUser == null)
        {
            Debug.LogWarning("Recv Dir message ,but not find the user !! userId : " + dirInfo.userId);
        }
        else
        {
            tempUser.SetBisection();
            Debug.Log("Split userId :" + dirInfo.userId);
        }


    }



    private void m_OnSomeOneDeathBtn(object sender, EventArgs e)
    {
        Debug.Log("m_OnSomeOneDeathBtn");

        throw new NotImplementedException();
    }





    private PlayerBody m_FindUser(string userId)
    {
        return m_playerList.Find(user =>
        {
            return user.userId == userId;
        });
    }
}
